//logs.js
const util = require('../../utils/util.js')
import request from "../../utils/request";
var api = require("../../utils/api");
const app = getApp()
import {
  formatTime
} from '../../utils/util.js';
var innerAudioContext = null
Page({
  data: {
    resultContent: '',
    sun: 0,
    time: 0,
    imageList: [],
    fileArr: [], // 文件名字和url
    typeId: '',
    onActiveId:null
  },
  onLoad(option) {
    var postId = option.rowId;
    this.setData({
      typeId: postId
    })
  },
  onUnload(){
    innerAudioContext.destroy()
  },
  onBossBtn() {
    innerAudioContext.destroy()
    wx.navigateTo({
      url: '/pages/boss/boss?rowId=' + this.data.typeId,
    })
  },
  onBossEdit(){
    app.globalData.nexPageData = this.data.details
    innerAudioContext.destroy()
    wx.navigateTo({
      url: '/pages/boss/boss?rowId=' + this.data.typeId,
    })
  },
  // 播放语音
  startVoice(e){
      var self = this
      var a1 = 0
      var a2 = 0
      var a3 = 0
      var a4 = 0
      var playItem = e.currentTarget.dataset.item
      var lastPlayItem = ''
      innerAudioContext.src = playItem
      innerAudioContext.obeyMuteSwitch = false
      innerAudioContext.play()
      this.setData({
        onActiveId: e.currentTarget.dataset.id
      })
      innerAudioContext.onPlay(() => {
        console.log('开始播放')
      })
      innerAudioContext.onEnded(() => {
        console.log('播放结束')
        self.setData({
          onActiveId: null
        })
        innerAudioContext.stop()
      })
      // innerAudioContext.onStop(() => {
      //   console.log('播放tz')
      //   self.setData({
      //     onActiveId: null
      //   })
      //   innerAudioContext.stop()
      // })
      innerAudioContext.onError((res) => {
        self.setData({
          onActiveId: null
        })
        innerAudioContext.stop()
      })
  },
  onShow(){
    debugger
  innerAudioContext = wx.createInnerAudioContext()

    if (this.data.typeId) {
      this.fetchData(this.data.typeId);
    }
  },
  onFile(e) {
    innerAudioContext.destroy()
    var fileUrl = e.currentTarget.dataset.fileurl
    //检查是否为文档doc, xls, ppt, pdf, docx, xlsx, pptx
    var isDoc = (/.doc$|.xls$|.ppt$|.docx$|.xlsx$|.pptx$|.pdf$/i)
    if (isDoc.test(fileUrl)) {
      wx.downloadFile({
        url: fileUrl,
        success: function(res) {
          var filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            success: function(res) {
              console.log('打开文档成功' + filePath)
            }
          })
        }
      })
    } else {
      wx.navigateTo({
        url: '/pages/details/details/webview/webview?fileUrl=' + fileUrl,
      })
    }
  },

  fetchData(id) {
    var url = api.SecretDetails
    var params = {
      id: id,
      isadmin: app.globalData.loginInfo.isadmin
    }
    request(url, params, "POST").then(res => {
      if (res.code == 0) {
        var result = res.data;
        var status = res.data.check_status;
        var oneImg = res.data.file;
        var data = formatTime(new Date(res.data.check_time * 1000));
        var a = res.data.append_bonus;
        var b = res.data.bonus;
        var sum = (a + b);
        var fileArr = res.data.file.split(',')
        var isImg = (/.jpg$|.png$|.jpeg$|.gif$/i)
        var imgArr = []
        fileArr.forEach((item, id) => {
          var obj = {}
          var isImg = (/.jpg$|.png$|.jpeg$|.gif$/i)
          if (item) {
            if (isImg.test(item)) {
              imgArr.push(item)
            } else {
              imgArr.push('/imgs/file.png')
            }
          }
        })
        var files = this.resolveFileStr(result)
        var resultContent = util.escape2Html(result.content)  
        this.setData({
          resultContent: resultContent,
          details: result,
          status: status,
          imageList: oneImg,
          fileArr: files,
          time: data,
          sun: sum,
          isBoss: app.globalData.isBoss
        })
      }
    })
  },
  resolveFileStr(e) {
    var fileArr = e.file.split(',')
    var allFile = []
    fileArr.forEach(item => {
      var obj = {
        name: '',
        url: '',
      }
      var lastStr = item.split('/')
      obj.name = lastStr[lastStr.length - 1]
      obj.url = item
      allFile.push(obj)
    })
    return allFile
  },
  previewImage(e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  }
})